package com.lanju.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.lanju.common.SpringUtils;
import com.lanju.dao.AgreementDao;
import com.lanju.dao.HouseDao;
import com.lanju.entity.Agreement;
import com.lanju.entity.House;
import com.lanju.model.ResultModel;
import com.lanju.service.AgreementService;


@Service
public class AgreementServiceImpl implements AgreementService{

    private static final Logger LOGGER = LoggerFactory.getLogger(AgreementServiceImpl.class);
    
    @Autowired
    AgreementDao agreementDao;
    
    @Autowired
    HouseDao houseDao;

    
    /** 根据id修改 */
    @Override
    @Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
    public ResultModel edit(Agreement agreement) {
        Agreement obj = agreementDao.getById(agreement.getAgreementId());
        if(obj==null){
            return ResultModel.fail("Agreement不存在", null);
        }
        agreement.setAgreementContent(obj.getAgreementContent());
        try {
        	BeanUtils.copyProperties(agreement,obj,SpringUtils.getNullPropertyNames(agreement));
            agreementDao.merge(obj);
            return ResultModel.success("编辑成功", null);
        } catch (Exception e) {
            LOGGER.error("editAgreement fail",e);
        }
        return ResultModel.fail("编辑失败", null);
    }
    
    /** 根据id修改 */
    @Override
    @Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
    public ResultModel editAgreementContent(Agreement agreement) {
        Agreement obj = agreementDao.getById(agreement.getAgreementId());
        if(obj==null){
            return ResultModel.fail("Agreement不存在", null);
        }
        obj.setAgreementContent(agreement.getAgreementContent());
        if(agreement.getStatus()!=null){
        	obj.setStatus(agreement.getStatus());
        }
        try {
            agreementDao.merge(obj);
            return ResultModel.success("编辑成功", null);
        } catch (Exception e) {
            LOGGER.error("editAgreement fail",e);
        }
        return ResultModel.fail("编辑失败", null);
    }

    /** 根据id获取 */
    @Override
    @Transactional(value="financialmarketingTM", readOnly=true)
    public Agreement getById(String id) {
        Agreement obj = agreementDao.getById(id);
        return obj;
    }
    
    /** 根据参数获取 */
    @Override
    @Transactional(value="financialmarketingTM", readOnly=true)
    public Agreement getByParam(Agreement agreement) {
        Agreement obj = agreementDao.getByParam(agreement);
        return obj;
    }
    
    

    /** 分页查询 */
    @Override
    @Transactional(value="financialmarketingTM", readOnly=true)
    public Map<String, Object> getList(Map param, int pageSize, int offset) {
    	 Map<String, Object> rsMap = new HashMap<String, Object>();
    	 long count = agreementDao.getListCount(param);
         List<Map> list = agreementDao.getList(param, pageSize, offset);
         rsMap.put("total", count);
         rsMap.put("rows", list);
        return rsMap;
    }
    
    
    /** 根据id修改 */
    @Override
    @Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
    public ResultModel signName(Map param) {
        try {
	    	JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
			Agreement agreement = json.toJavaObject(Agreement.class);
	        Agreement obj = agreementDao.getById(agreement.getAgreementId());
	        if(obj==null){
	            return ResultModel.fail("系统未获取到该合同", null);
	        }
	        obj.setAgreementContent(agreement.getAgreementContent());
        	obj.setStatus(agreement.getStatus());
        	obj.setUpdateTime(new Date());
            agreementDao.merge(obj);
            
            House house = houseDao.findById(obj.getHouseId());
            String houseStatus = (String) param.get("houseStatus");
            house.setStatus(houseStatus);
            houseDao.update(house);
            
            return ResultModel.success("签约成功", null);
        } catch (Exception e) {
            LOGGER.error("editAgreement fail",e);
        }
        return ResultModel.fail("签约失败", null);
    }

	@Override
	public Map<String, Object> getAllAgreementList(Map<String, Object> map) {
		
		return null;
	}

	@Override
	public Map<String, Object> getAgreementById(Map<String, Object> map) {
		Map<String, Object> result=new HashMap<>();
		Agreement a=agreementDao.getAgreementById(map);
		result.put("rows",a);
		return result;
	}

	@Override
	public List<Agreement> getAgreementList(Map<String, Object> map) {
		return agreementDao.getAgreementList(map);
	}
    
  
}
